﻿//!CompilerOption:Optimize:On
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ServiceModel;
using System.Windows;
using System.Windows.Threading;
using Winz.HBRelog.Remoting;





namespace Winz.HBRelog.Remoting
{
    [ServiceContract]
    interface IRemotingApi
    {
        [OperationContract]
        bool Init(int hbProcID);
        [OperationContract(IsOneWay = true)]
        void RestartHB(int hbProcID);
        [OperationContract(IsOneWay = true)]
        void RestartWow(int hbProcID);
        [OperationContract]
        string[] GetProfileNames();
        [OperationContract]
        string GetCurrentProfileName(int hbProcID);
        [OperationContract(IsOneWay = true)]
        void StartProfile(string profileName);
        [OperationContract(IsOneWay = true)]
        void StopProfile(string profileName);
        [OperationContract(IsOneWay = true)]
        void PauseProfile(string profileName);
        [OperationContract(IsOneWay = true)]
        void IdleProfile(string profileName, TimeSpan time);
        [OperationContract(IsOneWay = true)]
        void Logon(int hbProcID, string character, string server, string customClass, string botBase, string profilePath);
        [OperationContract]
        int GetProfileStatus(string profileName);
        [OperationContract(IsOneWay = true)]
        void SetProfileStatusText(int hbProcID, string status);
        [OperationContract(IsOneWay = true)]
        void SetBotInfoToolTip(int hbProcID, string tooltip);
        [OperationContract(IsOneWay = true)]
        void SkipCurrentTask(string profileName);
    }

}

namespace Winz.HBRelog
{
    public class HBRelogHelper
    {
        static internal IRemotingApi HBRelogRemoteApi { get; private set; }
        static DispatcherTimer _monitorTimer;
        static ChannelFactory<IRemotingApi> _pipeFactory;
        static internal HBRelogHelper Instance { get; private set; }
        List<string> PausedProfileNames;

        public HBRelogHelper()
        {
            Instance = this;
            PausedProfileNames = new List<string>();
            try
            {

                _pipeFactory = new ChannelFactory<IRemotingApi>(new NetNamedPipeBinding(),
                        new EndpointAddress("net.pipe://localhost/HBRelog/Server"));

                HBRelogRemoteApi = _pipeFactory.CreateChannel();
            }
            catch (Exception ex)
            {
                Log.Debug("[Erreur] Source :{0} Msg:{1} {2}", ex.Source, ex.Message, ex.StackTrace);
            }
        }

        public void Pause() {
            try
            {
                PausedProfileNames = (new List<string>(HBRelogApi.GetProfileNames())).FindAll(s => HBRelogApi.GetProfileStatus(s) == 2); // Running
                PausedProfileNames.ForEach(p =>
                        {
                            Log.Debug("[Pausing ] {0}",p);
                            HBRelogApi.PauseProfile(p);
                        });
            }
            catch(Exception ex)
            {
                PausedProfileNames = new List<string>();
                Log.Debug("[Erreur] Source :{0} Msg:{1} {2}", ex.Source, ex.Message, ex.StackTrace);
            }
             
            // Kill wow
            Log.Debug("Killing Wow Processes ({0 found}", System.Diagnostics.Process.GetProcessesByName("Wow").Length);
            foreach (Process p in System.Diagnostics.Process.GetProcessesByName("Wow"))
            {
                Log.Debug("[Wow] Id : {0} , Name : {1}", p.Id, p.ProcessName);
                p.Kill();
                p.WaitForExit();
            }


        }


        public void Restart()
        {
            try
            {
                foreach (string p in PausedProfileNames)
                {
                    HBRelogApi.StartProfile(p);
                };
                PausedProfileNames = new List<string>(); ;
            }
            catch(Exception ex)
            {
                Log.Debug("[Erreur] Source :{0} Msg:{1} {2}", ex.Source, ex.Message, ex.StackTrace);
            }
        }

    }

    static public class HBRelogApi
    {
        static IRemotingApi HBRelogRemoteApi { get { return HBRelogHelper.HBRelogRemoteApi; } }
        public static string[] GetProfileNames() { return HBRelogRemoteApi.GetProfileNames(); }
        
        public static string GetProfileName(int hbProcID) {return HBRelogRemoteApi.GetCurrentProfileName(hbProcID); }
        public static void StartProfile(string profileName) { HBRelogRemoteApi.StartProfile(profileName); }
        public static void StopProfile(string profileName) { HBRelogRemoteApi.StopProfile(profileName); }
        public static void PauseProfile(string profileName) { HBRelogRemoteApi.PauseProfile(profileName); }
        public static void IdleProfile(string profileName, TimeSpan time)
        {
            HBRelogRemoteApi.IdleProfile(profileName, time);
        }


        public static int GetProfileStatus(string profileName)
        {
            return HBRelogRemoteApi.GetProfileStatus(profileName);
        }


        public static void SkipCurrentTask(string profileName)
        {
            HBRelogRemoteApi.SkipCurrentTask(profileName);
        }

        
    }
}